package com.wiser.ohoscard;

import com.wiser.ohoscard.db.Form;
import com.wiser.ohoscard.db.FormDatabase;
import com.wiser.ohoscard.utils.ComponentProviderUtils;
import com.wiser.ohoscard.utils.DatabaseUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentProvider;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.event.notification.NotificationRequest;
import ohos.rpc.IRemoteObject;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class TimerAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    private DatabaseHelper helper = new DatabaseHelper(this);

    private OrmContext connect;

    @Override
    public void onStart(Intent intent) {
        HiLog.error(LABEL_LOG, "TimerAbility::onStart");
        super.onStart(intent);
        connect = helper.getOrmContext("FormDatabase", "FormDatabase.db", FormDatabase.class);
        startTimer();
    }

    // 定时更新服务卡片UI
    private void startTimer() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                updateForm();
                notice();
            }
        },1000,1000);
    }

    // 防止服务被杀死
    private void notice() {
        // 前台服务
        NotificationRequest request = new NotificationRequest(101010);
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle("前台服务卡片标题");
        content.setText("服务卡片前台服务");
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        request.setContent(notificationContent);
        request.setAlertOneTime(true);
        keepBackgroundRunning(1001,request);
    }

    // 更新服务卡片数据以及UI
    private void updateForm() {
        OrmPredicates ormPredicates = new OrmPredicates(Form.class);
        List<Form> formList = connect.query(ormPredicates);
        if (formList.size() <= 0) return;
        for(Form form: formList) {
            ComponentProvider componentProvider = ComponentProviderUtils.getComponentProvider(form,this);
            try {
                Long updateFormId = form.getFormId();
                updateForm(updateFormId, componentProvider);
            } catch (FormException e) {
                // 删除不存在的卡片
                DatabaseUtils.deleteFormData(form.getFormId(), connect);
                HiLog.error(LABEL_LOG, "onUpdateForm updateForm error");
            }
        }
    }

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LABEL_LOG, "TimerAbility::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        cancelBackgroundRunning();
        HiLog.info(LABEL_LOG, "TimerAbility::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
        super.onCommand(intent,restart,startId);
        notice();
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        return null;
    }

    @Override
    public void onDisconnect(Intent intent) {
    }
}